package common;

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import com.sun.management.OperatingSystemMXBean;


public class Cpu_moniter {
	 public static void main(String args[])throws Exception
	 {
		String DB_IP = "";
		String DB_PORT = "";
		String DB_NAME = "";
		String DB_USER = "";
		String DB_PASSWORD = "";
		String ConfigFile = "./dbconfig.db"; 
		
		Connection DBCon = null;
		Statement stmt;
		String inDate   = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
		String inTime   = new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date());
		float cpu = getCPU();
		if (cpu < Integer.parseInt(args[0])){
//			System.out.println(inDate+" "+ inTime +" : "+ cpu);
			try
			{
				Class.forName("com.mysql.jdbc.Driver");
			    Properties config = new Properties();
			    config.load(new FileInputStream(ConfigFile));
			    DB_IP = config.getProperty("DB_IP");
			    DB_PORT = config.getProperty("DB_PORT");
			    DB_NAME = Crypto.DecodeBySType(config.getProperty("DB_NAME"));
			    DB_USER = Crypto.DecodeBySType(config.getProperty("DB_USER"));
			    DB_PASSWORD = Crypto.DecodeBySType(config.getProperty("DB_PASSWORD"));

			    DBCon = DriverManager.getConnection("jdbc:mysql://"+ DB_IP +":"+ DB_PORT +"/"+ DB_NAME ,DB_USER,DB_PASSWORD);
			    stmt = DBCon.createStatement();
			    String SMSSTATE = "N";
			    if (cpu < 50) SMSSTATE = "Y";
			    stmt.executeQuery("call CPU_STATE_INSERT('"+ inDate+" "+ inTime +"',"+ cpu +",'"+ SMSSTATE+"')");
			    stmt.close();
			    DBCon.close();
			    System.out.println(inDate +" "+ inTime +":CPU= "+ cpu);
			  }catch(IOException e)
			  {	    
					System.out.println(inDate +" "+ inTime +":IOException= "+e.getMessage());
			  }catch(SQLException e)
			  {
					System.out.println(inDate +" "+ inTime +":SQLException= "+e.getMessage());
			  }finally {
					try{
						if (DBCon!=null) DBCon.close();
					}catch(Exception ee){}
			}			
		}else{
			System.out.println(inDate +" "+ inTime +":CPU= "+ cpu);
		}
	 }
	 public static float getCPU() {
	        OperatingSystemMXBean osbean = (OperatingSystemMXBean) ManagementFactory
	                .getOperatingSystemMXBean();
	        RuntimeMXBean runbean = (RuntimeMXBean) ManagementFactory
	                .getRuntimeMXBean();

	        long bfprocesstime = osbean.getProcessCpuTime();
	        long bfuptime = runbean.getUptime();

	        long ncpus = osbean.getAvailableProcessors();

	        for (int i = 0; i < 1000000; ++i) {
	            ncpus = osbean.getAvailableProcessors();
	        }

	        long afprocesstime = osbean.getProcessCpuTime();
	        long afuptime = runbean.getUptime();

	        float cal = (afprocesstime - bfprocesstime)
	                / ((afuptime - bfuptime) * 10000f);

	        float usage = Math.min(99f, cal);
	        return usage;

	    }
}
